/*
 * 文件名：StsQaJobCoreServiceImpl5_6.java
 * 版权：Copyright by www.yunshows.com
 * 描述：
 * 修改人：Awoke
 * 修改时间：2020年2月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.exception.ServiceException;
import top.infopub.job.fia.service.FeeNoteService;
import top.infopub.job.fia.service.SuperBudgetOfgroupDtlService;
import top.infopub.job.fia.service.SuperBudgetOfgroupService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.PreProjectService;
import top.infopub.job.sts.service.OrgAreaQaService;
import top.infopub.job.sts.service.OrgProQaService;
import top.infopub.job.sts.service.OrgSuperQaService;
import top.infopub.job.sts.service.StsQaJobCoreService5_6;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.fia.FeeNote;
import top.infopub.model.fia.SuperBudgetOfgroup;
import top.infopub.model.fia.SuperBudgetOfgroupDtl;
import top.infopub.model.sts.OrgAreaQa;
import top.infopub.model.sts.OrgSuperQa;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


/**
 * 经营质量分析报表 数据归集汇总核心service 为给job服务</br>
 * 5=管理费用  6=管理费率 </br>
 * 【重要修改】： 2020.5.12 只有总部有管理费用/费率   by Awoke
 * @author Awoke
 * @version 2020年2月15日
 * @see StsQaJobCoreService5_6
 * @since
 */
@Service
public class StsQaJobCoreServiceImpl5_6 implements StsQaJobCoreService5_6 {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OrgSuperQaService orgSuperQaService;

    @Autowired
    private OrgAreaQaService orgAreaQaService;

    @Autowired
    private OrgProQaService orgProQaService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private FeeNoteService feeNoteService;

    @Autowired
    private SuperBudgetOfgroupService superBudgetOfgroupService;

    @Autowired
    private SuperBudgetOfgroupDtlService superBudgetOfgroupDtlService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfMgrFeeRateJob() {
        /*
         * 现在管理费用/费率  只有总部有</br>
         * 管理费率 = 管理费用/投资额
         */

        // 新增- 总部公司经营质量 数据
        Map<String, Object> superOrgBudgetWhere = Maps.newHashMap();
        superOrgBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superOrgBudgetWhere.put("processStatus", "9");
        // 【建投总部年度控股指标预算 fia_super_budget_ofgroup】{管理费用}
        List<SuperBudgetOfgroup> superOrgBudgetList = superBudgetOfgroupService.selectByMapWhere(superOrgBudgetWhere);
        // 预算不存在
        if (superOrgBudgetList == null || superOrgBudgetList.isEmpty()) {
            throw new ServiceException("[总部]经营质量分析指标-管理费用 数据归集时，建投总部预算不存在~");
        }
        SuperBudgetOfgroup var = superOrgBudgetList.get(0);

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(5);// 指标类型 5=管理费用
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

        // 当月计划
        Map<String, Object> orgBudgetDetailWhere = Maps.newHashMap();
        orgBudgetDetailWhere.put("orgBudgetId", var.getId());
        orgBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        SuperBudgetOfgroupDtl plan = superBudgetOfgroupDtlService.selectByMapWhere(
            orgBudgetDetailWhere).get(0);
        superQaAdd.setCurMonthPlan(plan.getManageFee());// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getManageFee()));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getManageFee()));
        // 年度计划
        superQaAdd.setYearAllPlan(var.getManageFeeSum());// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getManageFeeSum()));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getManageFeeSum()));

        // 当月实际完成
        FeeNote feeNoteWhere = new FeeNote();
        feeNoteWhere.setHrOrgBillId(superQaAdd.getHrOrgSuperId());
        feeNoteWhere.setBeeDateMonth(superQaAdd.getStsMonth());
        feeNoteWhere.setNoteType("2");// 2=管理费用
        feeNoteWhere.setState(1);
        List<FeeNote> feeNoteList = feeNoteService.selectByEntityWhere(feeNoteWhere);
        if (feeNoteList == null || feeNoteList.isEmpty()) {
            // 实际当月完成为0
            superQaAdd.setCurMonthFinish(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
        }
        else {
            BigDecimal unit = sumOf(feeNoteList);
            // 实际当月完成 
            superQaAdd.setCurMonthFinish(unit);
            superQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(unit));
            superQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(unit));
        }
        if (superQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setCurMonthRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
                superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }

        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 5);// 指标类型 5=管理费用
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        }
        else {
            OrgSuperQa prevSuperQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                prevSuperQa.getYearAddupPlanTts()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                prevSuperQa.getYearAddupPlanHms()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                prevSuperQa.getYearAddupFinish()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                prevSuperQa.getYearAddupFinishTts()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                prevSuperQa.getYearAddupFinishHms()));
            // 计算月累完成率
            if (superQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                superQaAdd.setYearAddupRate(BigDecimal.ZERO);
            }
            else {
                superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                    superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
        }
        // 年度完成率  月累完成/年度计划 
        if (superQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setYearAllRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
                superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);

        // 以下插入  总部公司 管理费率 = 管理费用合计/投资额合计
        
        OrgSuperQa superQaAddRate = new OrgSuperQa();
        superQaAddRate.setHrOrgSuperId(superQaAdd.getHrOrgSuperId());
        superQaAddRate.setAimType(6); // 管理费率
        superQaAddRate.setBelongsYear(superQaAdd.getBelongsYear());
        superQaAddRate.setBelongsMonth(superQaAdd.getBelongsMonth());
        superQaAddRate.setStsMonth(superQaAdd.getStsMonth());
        // 获得对应月份的 投资额 而不再是自行承接建安额 2020.5.12修改 by Awoke
        Map<String, Object> sameInvestWhere = Maps.newHashMap();
        sameInvestWhere.put("hrOrgSuperId", superQaAdd.getHrOrgSuperId());
        sameInvestWhere.put("aimType", 3);// 指标类型 3=投资额
        sameInvestWhere.put("belongsYear", superQaAdd.getBelongsYear());
        sameInvestWhere.put("belongsMonth", superQaAdd.getBelongsMonth());
        sameInvestWhere.put("orderCondition", " add_time desc ");
        List<OrgAreaQa> sameInvestList = orgAreaQaService.selectByMapWhere(sameInvestWhere);
        if (sameInvestList == null || sameInvestList.isEmpty()) {
            throw new ServiceException("[总部]经营质量分析指标-管理费率 数据归集时，取不到对应的投资额~");
        }
        OrgAreaQa sameInvest = sameInvestList.get(0);
        // 管理费率 = 管理费用合计/投资额
        if (sameInvest.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAddRate.setCurMonthPlan(BigDecimal.ZERO);
        }
        else {
            superQaAddRate.setCurMonthPlan(superQaAdd.getCurMonthPlan().divide(
                sameInvest.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAddRate.setCurMonthPlanTts(superQaAddRate.getCurMonthPlan());
        superQaAddRate.setCurMonthPlanHms(superQaAddRate.getCurMonthPlan());// 三者相等
        if (sameInvest.getCurMonthFinish().compareTo(BigDecimal.ZERO) == 0) {
            superQaAddRate.setCurMonthFinish(BigDecimal.ZERO);
        }
        else {
            superQaAddRate.setCurMonthFinish(superQaAdd.getCurMonthFinish().divide(
                sameInvest.getCurMonthFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAddRate.setCurMonthFinishTts(superQaAddRate.getCurMonthFinish());
        superQaAddRate.setCurMonthFinishHms(superQaAddRate.getCurMonthFinish());// 三者相等
        if (sameInvest.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAddRate.setYearAddupPlan(BigDecimal.ZERO);
        }
        else {
            superQaAddRate.setYearAddupPlan(superQaAdd.getYearAddupPlan().divide(
                sameInvest.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAddRate.setYearAddupPlanTts(superQaAddRate.getYearAddupPlan());
        superQaAddRate.setYearAddupPlanHms(superQaAddRate.getYearAddupPlan());// 三者相等
        if (sameInvest.getYearAddupFinish().compareTo(BigDecimal.ZERO) == 0) {
            superQaAddRate.setYearAddupFinish(BigDecimal.ZERO);
        }
        else {
            superQaAddRate.setYearAddupFinish(superQaAdd.getYearAddupFinish().divide(
                sameInvest.getYearAddupFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAddRate.setYearAddupFinishTts(superQaAddRate.getYearAddupFinish());
        superQaAddRate.setYearAddupFinishHms(superQaAddRate.getYearAddupFinish());// 三者相等
        if (sameInvest.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAddRate.setYearAllPlan(BigDecimal.ZERO);
        }
        else {
            superQaAddRate.setYearAllPlan(superQaAdd.getYearAllPlan().divide(
                sameInvest.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        }
        superQaAddRate.setYearAllPlanTts(superQaAddRate.getYearAllPlan());
        superQaAddRate.setYearAllPlanHms(superQaAddRate.getYearAllPlan());// 三者相等

        superQaAddRate.setAddBy("system");
        superQaAddRate.setAddTime(new Date());
        superQaAddRate.setUptBy("system");
        superQaAddRate.setUptTime(new Date());
        orgSuperQaService.insert(superQaAddRate);
    }

    /**
     * 合计费用
     */
    private BigDecimal sumOf(List<FeeNote> feeNoteList) {
        BigDecimal unit = BigDecimal.ZERO;
        for (FeeNote var : feeNoteList) {
            unit = unit.add(var.getBeeAmount());
        }
        return unit;
    }

}
